local WeaponShadow = Class(function(self, inst)
	self.inst = inst

	self.shadow_per_frame = 1
	self.shadow_num = 0

	self.stack = {}
	self.stack_capacity = 16

	self._ison = net_bool(inst.GUID, "oa_weaponshadow._ison", "oa_weaponshadow._ison")
	self._ison:set_local(false)

	-- inst:ListenForEvent()
end)

function WeaponShadow:SetSpawnPrefabFn(fn)
	self.spawnprefabfn = fn 
end

function WeaponShadow:SetSleepFn(fn)
	self.sleepfn = fn 
end

function WeaponShadow:SetAwakeFn(fn)
	self.awakefn = fn 
end

function WeaponShadow:SpawnPrefab()
	if #self.stack > 0 then
		local ent = table.remove(self.stack, #self.stack)
		if self.awakefn then
			self.awakefn(ent)
		end 
		return ent
	end 
	if self.spawnprefabfn ~= nil then
		return self.spawnprefabfn()
	end
end

function WeaponShadow:OnUpdate()
	self.shadow_num = self.shadow_num + self.shadow_per_frame

	while self.shadow_num > 1 do
		self.shadow_num = self.shadow_num - 1
		local ent = self:SpawnPrefab()
		ent.PushToStack = function(inst)
			if #self.stack == self.stack_capacity then 
				inst:Remove()
			else
				if self.sleepfn then
					self.sleepfn(inst)
				end
				table.insert(self.stack, ent)
			end
		end
	end
end

function WeaponShadow:Start()
	self.inst:StartUpdatingComponent(self)
	if TheWorld.ismastersim then
		self._ison:set(true)
	end
end

function WeaponShadow:Stop()
	self.inst:StopUpdatingComponent(self)
	if TheWorld.ismastersim then
		self._ison:set(false)
	end
end

function WeaponShadow:GetDebugString()
	return string.format("STACK: %d/%d", #self.stack, self.stack_capacity)
end

return WeaponShadow